blog

Home / DeveloperSection / Blogs / Abstraction in c# with example

Abstraction in c# with example

AVADHESH PATEL69813 05-Jul-2012

In object-oriented software, complexity is managed by using abstraction. Abstraction is a process that involves identifying the critical behavior of an object and eliminating irrelevant and complex denials. Abstraction is a process of identifying the relevant qualities and behaviors an object should possess.

Example-A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, USB ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works.  You just need to know how to operate the laptop by switching it on.

Note- When derived class inherited with abstract class; derived class must be override abstract class methods.


Example of Abstraction:

using System;
using System.Collections.Generic;
using System.Linq;
namespace abstarction
{
    public abstract class university
    {
        public abstract void BTech();
        public abstract void MBA();
    }
    public class GBTU : university
    {
        public override void BTech()
        {
            Console.WriteLine("GBTU BTech Fee 50000/-");
        }
        public override void MBA()
        {
            Console.WriteLine("GBTU MBA Fee 100000/-");
        }
    }
    public class MTU : university
    {
        public override void BTech()
        {
            Console.WriteLine("MTU BTech Fee 40000/-");
        }
      public override void MBA()
        {
            Console.WriteLine("MTU MBA Fee 800000/-");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            GBTU g = new GBTU();
            g.BTech();
            g.MBA();
            MTU m = new MTU();
            m.BTech();
            m.MBA();
            Console.ReadLine();
        }
    }
}
Output-

GBTU BTech Fee 50000/-

GBTU MBA Fee 100000/-

MTU BTech Fee 40000/-

MTU MBA Fee 800000/-

Explanation :-

 1-From above example we have make one abstract class university and two abstract methods Btech and MBA. GBTU and MTU both are override university course fee.

2- University course common for both GBTU and MTU so university method BTech and MBA is abstract.

3-GBTU and MTU inherited abstract method so university method must be override here.


Updated 18-Sep-2014
Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

Comments

Liked By